#include "checksum.h"
#include <stdio.h>
#include <string.h>

int min(int x, int y) {
	return (x < y)?x:y;
}

unsigned short checksum (unsigned char *buf, int nbytes)
{
	int i = 0;
	unsigned int sum = 0;
	printf("\n");
	while (i+1 < nbytes) {
		sum += buf[i] << 8;
		sum += buf[i+1];
		sum = (sum & 0xffff) + (sum >> 16);
		i += 2;
	}
	if(i < nbytes) {
		sum += buf[i];
		sum = (sum & 0xffff) + (sum >> 16);
	}
	return (unsigned short) ~sum;
}
